﻿package com.vfd.animation{
	import com.vfd.animation.tweens.DelayedTween;
	import flash.display.Sprite;
	import fl.motion.Color;
	import flash.geom.ColorTransform;
	import fl.transitions.TweenEvent;
	import flash.events.Event;
	public class ClipAnimator {
		private var _clip:Sprite;
		private var originalX:Number;
		private var originalY:Number;
		/*tweens*/
		private var _xTween:DelayedTween;
		private var _yTween:DelayedTween;
		private var _alphaTween:DelayedTween;
		private var _rotationTween:DelayedTween;
		private var _widthTween:DelayedTween;
		private var _heightTween:DelayedTween;
		private var colorTween:DelayedTween;
		private var tweenArray:Array;
		/*end tweens*/
		private var colorTrans:ColorTransform;
		private var tweenToColor:Number;
		private var lastColor:Number;
		private var timerClip:Sprite;
		private var orbitAngle:Number = 0;
		private var _orbitVelocity:Number = .5;
		private var _orbitY:Number;
		private var _orbitX:Number;
		private var _orbitDistance:Number;
		public function ClipAnimator(clip:Sprite) {
			trace("New clip animator");
			this._clip = clip;
			this.originalX = this._clip.x;
			this.originalY = this._clip.y;
			this._orbitY = this._clip.stage.stageHeight/2;
			this._orbitX = this._clip.stage.stageWidth/2;
			this._xTween =  new DelayedTween(this._clip , "x", null, this._clip.x, this._clip.x, 1, true);
			this._xTween.stop();
			this._yTween =  new DelayedTween(this._clip , "y", null, this._clip.y, this._clip.y, 1, true);
			this._yTween.stop();
			this._alphaTween =  new DelayedTween(this._clip , "alpha", null, this._clip.alpha, this._clip.alpha, 1, true);
			this._alphaTween.stop();
			this._rotationTween =  new DelayedTween(this._clip , "rotation", null, this._clip.rotation, this._clip.rotation, 1, true);
			this._rotationTween.stop();
			this._widthTween =  new DelayedTween(this._clip , "width", null, this._clip.width, this._clip.width, 1, true);
			this._widthTween.stop();
			this._heightTween =  new DelayedTween(this._clip , "height", null, this._clip.height, this._clip.height, 1, true);
			this._heightTween.stop();
			this.timerClip = new Sprite();
			this.colorTween = new DelayedTween(this.timerClip,'alpha',null,0,1,1,true);
			this.colorTween.stop();
			this.tweenArray = new Array(this._xTween, this._yTween, this._alphaTween, this._rotationTween, this._widthTween, this._heightTween,this.colorTween);
			this.colorTrans = this._clip.transform.colorTransform;
			this._orbitDistance  = this._clip.width*2;
			this.colorTween.addEventListener(TweenEvent.MOTION_CHANGE, updateColor);


		}
		public function set delay(seconds:Number) {
			for (var tw in this.tweenArray) {
				tweenArray[tw].delay = seconds;
			}
		}
		public function resetPosition():void {
			this._clip.x = this.originalX;
			this._clip.y = this.originalY;
		}
		public function moveToPoint(x:Number, y:Number,duration:Number = 0, loop:Boolean = false):void {
			this._xTween.finish = x;
			this._xTween.duration = duration;
			this._xTween.looping = loop;
			this._yTween.finish = y;
			this._yTween.duration = duration;
			this._yTween.looping = loop;
			this._xTween.start();
			this._yTween.start();
		}
		public function fadeTo(val:Number =0, duration:Number = 0, loop:Boolean = false):void {
			this._alphaTween.finish = val;
			this._alphaTween.duration = duration;
			this._alphaTween.looping=loop;
			this._alphaTween.start();
		}
		public function sizeTo(w:Number, h:Number, duration:Number=0, loop:Boolean = false):void {
			this._widthTween.finish=w;
			this._widthTween.looping = loop;
			this._widthTween.duration = duration;
			this._heightTween.finish = h;
			this._heightTween.looping = loop;
			this._heightTween.duration = duration;
			this._widthTween.start();
			this._heightTween.start();

		}
		public function widthTo(width:Number, duration:Number, loop:Boolean = false):void {
			this._widthTween.finish =width;
			this._widthTween.looping = loop;
			this._widthTween.duration = duration;
			this._widthTween.start();
		}
		public function heightTo(height:Number, duration:Number, loop:Boolean = false):void {
			this._heightTween.finish =height;
			this._heightTween.looping = loop;
			this._heightTween.duration = duration;
			this._heightTween.start();
		}
		public function spinTo(rot:Number= 0, duration:Number = 0, loop:Boolean = false):void {
			this._rotationTween.finish=rot;
			this._rotationTween.duration = duration;
			this._rotationTween.looping = loop;
			this._rotationTween.start();
		}
		public function xTo(x:Number, duration:Number = 0, loop:Boolean = false):void {
			this._xTween.finish = x;
			this._xTween.duration = duration;
			this._xTween.looping = loop;
			this._xTween.start();
		}
		public function yTo(y:Number, duration:Number = 0, loop:Boolean = false):void {
			this._yTween.finish = y;
			this._yTween.duration = duration;
			this._yTween.looping = loop;
			this._yTween.start();
		}
		public function set easing(ease:Function):void {
			for (var tw in this.tweenArray) {
				tweenArray[tw].func = ease;
			}
		}
		public function colorTo(toColor:Number, duration:Number):void {
			this.lastColor = this._clip.transform.colorTransform.color;
			this.tweenToColor = toColor;
			this.colorTween.duration = duration;
			this.colorTween.start();
		}
		private function updateColor(event:TweenEvent):void {
			this.colorTrans.color = Color.interpolateColor(this.lastColor, this.tweenToColor,event.position);
			updateColorTransform();
		}
		public function set currentColor(clr:Number) {
			this.lastColor = clr;
			this.colorTrans.color= clr;
			updateColorTransform();

		}
		public function startOrbit():void {
			this._clip.addEventListener(Event.ENTER_FRAME,orbit);
		}
		public function stopOrbit():void {
			this._clip.removeEventListener(Event.ENTER_FRAME,orbit);
			this.orbitAngle = 0;
		}
		private function orbit(event:Event) {
			this._clip.y = this._orbitY + Math.sin(this.orbitAngle)*this._orbitDistance;
			this._clip.x = this._orbitX + Math.cos(this.orbitAngle)*this._orbitDistance;
			this.orbitAngle += this._orbitVelocity;
		}
		public function set yEasing(ease:Function) {
			this._yTween.func = ease;
		}
		public function set xEasing(ease:Function) {
			this._xTween.func = ease;
		}
		public function set heightEasing(ease:Function) {
			this._heightTween.func = ease;
		}
		public function set widthEasing(ease:Function) {
			this._widthTween.func = ease;
		}
		public function set alphaEasing(ease:Function) {
			this._alphaTween.func = ease;
		}
		public function set rotationEasing(ease:Function) {
			this._rotationTween.func = ease;
		}
		public function set orbitVelocity(velocity:Number) {
			this._orbitVelocity = velocity;
		}
		public function set orbitY(y:Number) {
			this._orbitY = y;
		}
		public function set orbitX(x:Number) {
			this._orbitX = x;
		}
		public function set orbitDistance(distance:Number) {
			this._orbitDistance = distance;
		}
		public function get currentColor():Number {
			return this.lastColor;
		}
		private function updateColorTransform() {
			this._clip.transform.colorTransform = this.colorTrans;
		}
		public function get xTween() {
			return _xTween;
		}
		public function get yTween() {
			return _yTween;
		}
		public function get alphaTween() {
			return _alphaTween;
		}
		public function get rotationTween() {
			return _rotationTween;
		}
		public function get widthTween() {
			return _widthTween;
		}
		public function get heightTween() {
			return _heightTween;
		}
	}
}